<!--参数修改-单币对-->
<template>
  <div class="bg bg1">
    <scroller class="scroller">
      <div class="wrapper" v-if="pairMap">

        <!--信号-->
        <VSignal v-if="!signalNod[strategyName]" class="set" ref="signal" v-model="signal" />
        
        <!--参数列表-->
        <VParam class="param" ref="param" 
        v-model="senior" 
        :isModify="true"
        :isRestore="false"
        :reserves="reserves" 
        :investCoin="pairMap.investCoin" 
        :pairMap="pairMap"></VParam>

        <!--其他设置-->
        <VSet class="set" ref="set" v-model="sets" :pairMap="pairMap" @open="setOpen"></VSet>
                
        <!--开放跟单-->
        <VSwitc class="follow" ref="follow" v-if="sets && sets.isOpen && showFollow" 
        v-model="isOpenFollow" 
        :title="T('开放策略跟单')" 
        @change="followChange"></VSwitc>

        <div class="height80"></div>
      </div>
    </scroller>

    <!--启动-->
    <div class="ok bg3">
      <div class="ok-but main1" @click="okClick">
        <text class="font32 text1">{{T('修改参数')}}</text>
      </div>
    </div>
  </div>
</template>

<script>

//组件

import VSignal from "@/up/view/VSignal.vue";
import VParam from "@/up/view/VParam.vue";
import VSet from "@/up/view/VSet.vue";
import VSwitc from "@/public/view/VSwitc.vue";

//功能
import navigator from "@/_js/navigator"; 
import modal from "@/_js/modal";
import math from "@/_js/math";
import amStrategy2 from "@/strategy/amStrategy2";
import broadcast from '@/_js/broadcast';

import weexHttp from "@/_js/https"; ;
const weexFunction = weex.requireModule("WeexFunction");
const dom = weex.requireModule('dom')

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T('参数'));

export default {

  components: {
    VSignal,
    VParam,
    VSet,
    VSwitc,
  },

  data() {
    return {
      T:T,
      math:math,

      executeId:'',
      strategyName:'', //策略名称
      states:undefined, //执行状态
      configMap:undefined, //执行配置
      followMap:undefined, //跟单配置

      isOpenFollow:false, //turn 开放跟单
      followMimic:false, //turn 模拟盘开放跟单
      
      /************************************ 信号设置*/
      signal:undefined, //信号
      signalNod:{'DCA智能策略':1},

      /************************************ 参数设置*/
      reserves:undefined, //参数数组
      pairMap:undefined, //交易对

      /************************************ 其他设置*/
      sets:undefined, //其他设置

      /************************************ 切换选中*/
      senior: true, //true 显示参数设置
      showFollow: false, //true 显示跟单
    };
  },

  mounted() {

    this.executeId = navigator.getParam('executeId');
    this.strategyName = navigator.getParam('strategyName');
    this.states = navigator.getParam('states');
    
    this.configMap = navigator.getParam('configMap');
    this.followMap = navigator.getParam('followMap');
    this.isOpenFollow = navigator.getParam('isOpenFollow');
    this.followMimic = navigator.getParam('followMimic');
    
    /************************************ 信号设置设置*/
    var signalStates = navigator.getParam('signalStates');
    var signal = this.configMap.signal;
    if (signal) {
     signal.openSignalEdit = signalStates==0;
    }
    this.signal = signal;
    
    /************************************ 参数设置*/
    this.reserves = this.configMap.reserves;
    var pairMap = this.configMap.tradePairs[0];

    /************************************ 其他设置*/
    var stopProfit = this.configMap.stopProfit.length ? math.cheng(this.configMap.stopProfit, '100') : '';
    var stopLoss = this.configMap.stopLoss.length ? math.cheng(this.configMap.stopLoss, '-100') : '';

    var maxPrice = this.configMap.maxPrice;
    var minPrice = this.configMap.minPrice;
    var pauseTime = this.configMap.pauseTime;
    var stopTime = this.configMap.stopTime;

    var cancelOrder = this.configMap.cancelOrder;
    var isConvert = this.configMap.isConvert;
    
    this.sets = { 
                'isOpen':true, //true 显示
                'isEdit':true, //true 可编辑

                'stopProfit':stopProfit, //止盈率
                'stopLoss':stopLoss, //止损率

                'maxPrice':maxPrice, //价格上限终止
                'minPrice':minPrice, //价格下限终止

                'pauseTime':pauseTime, //暂停时间戳
                'timeStopStamp':stopTime, //终止时间戳
                
                'cancelOrder':cancelOrder, //true 撤销订单
                'isConvert':isConvert //true 兑换币种
              }
    this.showFollow = pairMap.apiId != '' || (pairMap.apiId == '' && this.followMimic);

    this.httpQuerySinglePair(pairMap);
  },

  methods: {

    /********************************** http*/
    //http交易对信息
    httpQuerySinglePair(pairMap) {

      modal.showLoad();
      var param = {exchangeId: pairMap.exchangeId,
                  counterCoinId: pairMap.counterCoinId,
                  baseCoinId: pairMap.baseCoinId};
      weexHttp.post("quoteV3/querySinglePair",param,true,(data) => {
          modal.hideLoad();
          if (data.status == 200) {
            data = data.data;
            pairMap = Object.assign(pairMap, data);
            pairMap['baseCoinUsdPrice'] = math.chu(pairMap.usdPrice, pairMap.last, math.getNumber(pairMap.baseCoinDecimal));
            this.pairMap = pairMap;
          } else {
            modal.toast(data.msg);
          }
        }
      );
    },

    //关闭跟单
    httCploseFollowStrategy(){

      modal.showLoad();
      var param = {executeId:this.executeId};
      weexHttp.postX("follow/closeFollowStrategy",param,true,(data)=>{
      modal.toast('dsfadsaf');
        modal.hideLoad();
        if(data.status == 200){
          modal.toast(T('关闭成功'));
          broadcast.send(broadcast.EXECUTE_UPDATE);
        } else {
          this.isOpenFollow = true;
          modal.toast(data.msg);
        }
      });
    },
    /********************************** 功能*/
    //开启跟单
    openFollow(){
      setTimeout(() => {
        this.isOpenFollow = false;
      }, 100);

      var parm = {executeId:this.executeId,
                  followName:this.strategyName,
                  pairMap:this.pairMap,
                  configMap:this.configMap,
                  followMap:this.followMap};
      navigator.open('','strategy/LaunchFollw', parm);
      broadcast.subscribeOnce('LaunchFollw', ()=>{
        this.isOpenFollow = true;
      });
    },

    //关闭跟单
    closeFollow(){ 

      var parm = {title:T("关闭跟单"),
                  message:T("关闭跟单后，跟单列表中将不可见此策略，原有跟随者不受影响"),
                  okBtn:T("确认"), cancelBtn:T("取消")};
      modal.confirm(parm, (bool)=>{
        if (bool == "true") {
          this.httCploseFollowStrategy();
        } else {
          this.isOpenFollow = true;
        }
      });
    }, 

    /********************************** 点击触发*/
    //其他设置-展开
    setOpen() {
      setTimeout(() => {
        if (this.$refs.follow) {
          dom.scrollToElement(this.$refs.follow, {offset:0});
        } else if (this.$refs.set) {
          dom.scrollToElement(this.$refs.set, {offset:0});
        }
      }, 100);
    },

    //跟单开关
    followChange(isOpenFollow){
      if (isOpenFollow) {
        this.openFollow();
      } else {
        this.closeFollow();
      }
    },

    //修改参数
    okClick() {

      //信号设置
      if (this.$refs.signal && this.$refs.signal.checkError()) {
        return;
      }

      //参数设置
      if (this.$refs.param.checkError()) {
        return;
      }

      //其他设置
      if (this.$refs.set.checkError()) {
        return;
      }
      
      var signal =  this.$refs.signal && this.$refs.signal.getParam(); 
      var reserves = this.$refs.param.getParam(); 
      var setMap = this.$refs.set.getParam(); 
      
      var param = {'executeId':this.executeId,
                  'executeConfig':this.configMap,
                  'states':this.states,
                  
                  'signal':signal,
                  'reserves':reserves, 
                  'setMap':setMap};
      amStrategy2.modifyParam(param, ()=>{
          modal.toast(T('修改成功'));
          broadcast.send(broadcast.EXECUTE_UPDATE);
          navigator.back();
      });
    }
  }
}
</script>


<style src="@/_css/style.css"></style>
<style scoped>
.scroller {
  top: 0;
  right: 0;
  left: 0;
  z-index: 9;
}
.wrapper {
  width: 750px;
}

.param{
  margin-top: 60px;
  margin-left: 40px;
  margin-right: 40px;
}
.set{
  margin-top: 40px;
  margin-left: 40px;
  margin-right: 40px;
}
.follow{
  margin-top: 40px;
  margin-left: 40px;
  margin-right: 40px;
}

/*************** 启动*/
.ok{
  flex-direction: row;
  justify-content: center;
  width: 750px;
  height: 164px;
}
.ok-but{
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 670px;
  height: 80px;
  border-radius: 4px;
  margin-top: 28px;
}
.height80{
  height: 80px;
}
</style>